﻿package com.shawnblais.ui
{
    import com.gskinner.motion.*;
    import com.shawnblais.events.*;
    import com.shawnblais.utils.*;
    
    import fl.motion.easing.Cubic;
    import fl.motion.easing.Quadratic;
    
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;
    
    import gs.TweenMax;
    import gs.easing.Back;
    
    import org.papervision3d.materials.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.view.*;
    
    public class BasicMenu3D extends DisplayObject3D
    {
    	private var mh:MainHeader
    	private var mp:Main_Portfolio
    	private var mo:Main_OG
    	private var mll:Main_Lab
    	private var ma:Main_About
    	private var mah:LabHeader
    	private var mc:ContactHeader
    	private var mb:BackButton
    	private var maa:AboutHeader
    	private var maac:About_Content
    	private var mmt:mcScrollTip
    	private var mcs:mcSettings
    	private var mcts:mcStartButton
    	private var mee:MemoryGauge
    	private var mfeel:Main_Contact
    	
    	private var mctsl:Main_Lab
    	private var meel:Contact_Form
    	private var mfeell:Contact_Form_Content_24
    	private var mctscll:Lab_Dynamic
    	
    	private var mcdastl:Lab_Dynamic_Content_32
    	private var mpo:Portfolio_Dynamic
    	private var mpff:PortfolioHeader
    	
        protected var _container:DisplayObject3D;
        protected var _viewport:Viewport3D;
        protected var _header_mc:MovieClip;
        protected var _aButtonData:Array;
        protected var _headerPlane:DisplayObject3D;
        protected var _materialAnimations:Boolean = false;
        protected var _backButton_mc:MovieClip;
        protected var _backButton:Plane;
        protected var _dMaterialIndexes:Dictionary;
        public var numPlaneSegments:uint = 1;
        protected var _materialInteractions:Boolean = false;
        protected var _aMaterialMovies:Array;
        protected var _initialIndex:Number;
        public var hideContent:Boolean = false;
        protected var _dPlanes:Dictionary;
        public var enableBackButton:Boolean = false;
        public var restoreIndex:Number = 0;
        protected var _aMaterials:Array;
        protected var _contentPlanes:DisplayObject3D;
        protected var _selectedIndex:Number;
        protected var _headerMaterial:MovieMaterial;

        public function BasicMenu3D(v3:Viewport3D, index:Number = -1, d:DisplayObject3D = null) : void
        {
            this._viewport =v3
            this._initialIndex = index
            this._container =d;
            if (this._container)
            {
                this._container.addChild(this);
            }
            return;
        }// end function

        protected function onBackOut(event:MouseEvent = null) : void
        {
            new ColorTween(this._backButton_mc.content_mc, this._backButton_mc.content_mc.transform.colorTransform.color, 0);
            this._viewport.buttonMode = false;
            this._viewport.useHandCursor = false;
            return;
        }// end function

        protected function onButtonClick(event:MouseEvent) : void
        {
            if (this._dMaterialIndexes[event.target] == this._selectedIndex)
            {
                if (this._aButtonData[this._selectedIndex].url)
                {
                    navigateToURL(new URLRequest(this._aButtonData[this._selectedIndex].url), "_blank");
                }
                else
                {
                    dispatchEvent(new Event(MenuEvent.BUTTON_CLICKED, true));
                }
            }
            else
            {
                this.selectPlane(this._dMaterialIndexes[event.target]);
            }
            return;
        }// end function

        protected function onButtonOut(event:MouseEvent) : void
        {
            this._viewport.buttonMode = false;
            this._viewport.useHandCursor = false;
            if (this._dMaterialIndexes[event.target] == this._selectedIndex)
            {
                new GTween(this._dPlanes[event.target], 0.35, {z:-30}, {ease:Quadratic.easeOut});
            }
            return;
        }// end function

        protected function createMovieAsset(param1:String)
        { 
          var _loc_2:* = getDefinitionByName(param1) as Class;
            var _loc_3:* = new _loc_2; 
          
            return _loc_3;
        }// end function

        public function selectPlane(n:Number = 1) : void
        {
            var _loc_3:MovieClip = null;
            var _loc_4:int = 0;
            var _loc_5:Number = NaN;
            var _loc_6:GTween = null;
            var _loc_7:int = 0;
            var _loc_2:Number = 0.35;
            this.optimizePlanes(null, false);
            if (isNaN(this._selectedIndex) && !isNaN(n) && this.enableBackButton)
            {
                setTimeout(this.showBackButton, 500);
            }
            if (!isNaN(n))
            {
                if (!n || n < 0 || n > this._aMaterialMovies.length)
                {
                    n = 0;
                }
                if (this._selectedIndex != n)
                {
                    var _loc_8:* = n;
                    this.restoreIndex = n;
                    this._selectedIndex = _loc_8;
                    _loc_4 = 0;
                    while (_loc_4 < this._aMaterialMovies.length)
                    {
                        
                        _loc_3 = this._aMaterialMovies[_loc_4];
                        if (_loc_3.content_mc)
                        {
                            new GTween(_loc_3.content_mc, _loc_2, {alpha:1}, {ease:Quadratic.easeOut});
                        }
                        if (_loc_4 == n)
                        {
                            new GTween(this._dPlanes[_loc_3], _loc_2, {z:-30}, {ease:Back.easeOut, delay:_loc_2 - 0.15});
                           // new GTween(this._contentPlanes, _loc_2 * 1.5, {x:this._headerPlane.x - this._dPlanes[_loc_3].x}, {ease:Back.easeOut});
                         
                          TweenMax.to(this._contentPlanes, _loc_2 * 1.5, {x:this._headerPlane.x - this._dPlanes[_loc_3].x, ease: Back.easeOut})
                         
                            if (_loc_3.overlay)
                            {
                                new GTween(_loc_3.overlay, _loc_2, {alpha:0}, {ease:Quadratic.easeOut});
                            }
                            if (_loc_3.title_txt)
                            {
                                new GTween(_loc_3.title_txt, _loc_2, {x:5 + (_loc_3.width - _loc_3.title_txt.textWidth - 20)}, {ease:Quadratic.easeOut});
                            }
                            if (this._header_mc.bg_mc && this._aButtonData[_loc_4].color)
                            {
                                _loc_5 = this._header_mc.bg_mc.transform.colorTransform.color;
                                new ColorTween(this._header_mc.bg_mc, _loc_5, this._aButtonData[_loc_4].color);
                            }
                            this._selectedIndex = _loc_4;
                        }
                        else
                        {
                            if (_loc_4 > this._selectedIndex)
                            {
                                new GTween(this._dPlanes[_loc_3], _loc_2, {z:10 * (_loc_4 - this._selectedIndex)}, {ease:Back.easeOut, delay:_loc_2 - 0.15});
                            }
                            else
                            {
                                new GTween(this._dPlanes[_loc_3], _loc_2, {z:10 * (-(_loc_4 - this._selectedIndex))}, {ease:Back.easeOut, delay:_loc_2 - 0.15});
                            }
                            if (_loc_3.overlay)
                            {
                                new GTween(_loc_3.overlay, _loc_2, {alpha:0.35}, {ease:Quadratic.easeOut});
                            }
                            if (_loc_3.title_txt)
                            {
                                new GTween(_loc_3.title_txt, _loc_2, {x:5}, {ease:Quadratic.easeOut});
                            }
                        }
                        _loc_4++;
                    }
                    dispatchEvent(new Event(MenuEvent.BUTTON_SELECTED, true));
                }
            }
            else
            {
                this._selectedIndex = NaN;
                _loc_7 = 0;
                while (_loc_7 < this._aMaterialMovies.length)
                {
                    
                    _loc_3 = this._aMaterialMovies[_loc_7];
                    if (_loc_3.overlay)
                    {
                        new GTween(_loc_3.overlay, _loc_2, {alpha:0.35}, {ease:Quadratic.easeOut});
                    }
                    if (_loc_3.title_txt)
                    {
                        new GTween(_loc_3.title_txt, _loc_2, {x:5}, {ease:Quadratic.easeOut});
                    }
                    if (_loc_3.content_mc && this.hideContent)
                    {
                        new GTween(_loc_3.content_mc, _loc_2, {alpha:0}, {ease:Quadratic.easeOut});
                    }
                    _loc_6 = new GTween(this._dPlanes[_loc_3], _loc_2, {z:0}, {ease:Back.easeOut});
                    new GTween(this._contentPlanes, _loc_2 * 2, {x:this._headerPlane.x}, {ease:Back.easeOut});
                    _loc_7++;
                }
                _loc_6.addEventListener(Event.COMPLETE, this.optimizePlanes);
            }
            if (isNaN(this._selectedIndex) && this.enableBackButton)
            {
                this.hideBackButton();
            }
            return;
        }// end function

        protected function onBackClick(event:MouseEvent) : void
        {
            dispatchEvent(new Event(MenuEvent.BUTTON_CLICKED, true));
            this.onBackOut();
            return;
        }// end function

        protected function showBackButton():void
        {
            this._backButton.z = 100;
            new GTween(this._backButton, 0.3, {z:60, y:(-this._aMaterialMovies[0].height) / 2 - this._backButton_mc.height / 2 - 100, x:5}, {ease:Cubic.easeOut});
            new GTween(this._backButton, 0.5, {z:-30, y:(-this._aMaterialMovies[0].height) / 2 - this._backButton_mc.height / 2 - 5}, {ease:Cubic.easeOut, delay:0.3});
            this._backButton_mc.content_mc.buttonMode = true;
            this._backButton_mc.content_mc.useHandCursor = true;
            this._backButton_mc.content_mc.mouseChildren = false;
            this._backButton_mc.content_mc.addEventListener(MouseEvent.CLICK, this.onBackClick);
            this._backButton_mc.content_mc.addEventListener(MouseEvent.ROLL_OVER, this.onBackOver);
            this._backButton_mc.content_mc.addEventListener(MouseEvent.ROLL_OUT, this.onBackOut);
            return;
        }// end function

        protected function onBackOver(event:MouseEvent) : void
        {
        	
        	//click back button changed
          //  new ColorTween(this._backButton_mc.content_mc, this._backButton_mc.content_mc.transform.colorTransform.color, this._aButtonData[this._selectedIndex].color);
            this._viewport.buttonMode = true;
            this._viewport.useHandCursor = true;
            return;
        }// end function

        protected function hideBackButton()
        {
            new GTween(this._backButton, 0.3, {z:30, y:(-this._aMaterialMovies[0].height) / 2 - this._backButton_mc.height / 2 - 100}, {ease:Cubic.easeOut});
            new GTween(this._backButton, 0.5, {z:200, y:0, x:200}, {ease:Back.easeOut, delay:0.3});
            this._backButton_mc.content_mc.buttonMode = false;
            this._backButton_mc.content_mc.useHandCursor = false;
            this._backButton_mc.content_mc.removeEventListener(MouseEvent.CLICK, this.onBackClick);
            this._backButton_mc.content_mc.removeEventListener(MouseEvent.ROLL_OVER, this.onBackOver);
            this._backButton_mc.content_mc.removeEventListener(MouseEvent.ROLL_OUT, this.onBackOut);
            return;
        }// end function

        protected function onButtonOver(event:MouseEvent) : void
        {
            this._viewport.buttonMode = true;
            this._viewport.useHandCursor = true;
            return;
        }// end function

        public function setButtonListeners(param1:Boolean = true)
        {
            var _loc_3:MovieClip = null;
            var _loc_2:int = 0;
            while (_loc_2 < this._aMaterialMovies.length)
            {
                
                _loc_3 = this._aMaterialMovies[_loc_2];
                _loc_3.buttonMode = param1;
                _loc_3.useHandCursor = param1;
                _loc_3.mouseChildren = false;
                if (param1)
                {
                    _loc_3.addEventListener(MouseEvent.CLICK, this.onButtonClick);
                    _loc_3.addEventListener(MouseEvent.ROLL_OVER, this.onButtonOver);
                    _loc_3.addEventListener(MouseEvent.ROLL_OUT, this.onButtonOut);
                }
                else
                {
                    _loc_3.removeEventListener(MouseEvent.CLICK, this.onButtonClick);
                    _loc_3.removeEventListener(MouseEvent.ROLL_OVER, this.onButtonOver);
                    _loc_3.removeEventListener(MouseEvent.ROLL_OUT, this.onButtonOut);
                }
                _loc_2++;
            }
            return;
        }// end function

        public function buildMenu(str:String, ary:Array) : void
        {
            var _loc_5:* = undefined;
            var _loc_6:MovieMaterial = null;
            var _loc_7:Plane = null;
            var _loc_8:MovieMaterial = null;
            this._aButtonData = ary;
            this._header_mc = this.createMovieAsset(str);
            this._headerMaterial = new MovieMaterial(this._header_mc, true, false, false);
            this._headerMaterial.doubleSided = false;
            this._headerMaterial.smooth = true;
            this._headerMaterial.interactive = false;
            this._headerMaterial.animated = false;
            if (this._headerPlane)
            {
                removeChild(this._headerPlane);
            }
            this._headerPlane = new Plane(this._headerMaterial, this._header_mc.width, this._header_mc.height, this.numPlaneSegments, this.numPlaneSegments);
            addChild(this._headerPlane);
            this._headerPlane.createViewportLayer(this._viewport, true);
            this._headerPlane.y = 170;
            this._contentPlanes = new DisplayObject3D();
            addChild(this._contentPlanes);
            this._viewport.getChildLayer(this._contentPlanes);
            this._aMaterialMovies = [];
            this._aMaterials = [];
            this._dPlanes = new Dictionary(true);
            this._dMaterialIndexes = new Dictionary(true);
            var _loc_3:Number = 0;
            var _loc_4:int = 0;
            while (_loc_4 < this._aButtonData.length)
            {
                
                _loc_5 = this.createMovieAsset(this._aButtonData[_loc_4].src);
                if (_loc_5.content_mc && this.hideContent)
                {
                    _loc_5.content_mc.alpha = 0;
                }
                if (_loc_5.overlay)
                {
                    _loc_5.overlay.alpha = 0.5;
                }
                if (this._aButtonData[_loc_4].frameNum && _loc_5.content_mc)
                {
                    _loc_5.content_mc.gotoAndStop(this._aButtonData[_loc_4].frameNum);
                }
                _loc_6 = new MovieMaterial(_loc_5, true, false, true);
                this._aMaterialMovies.push(_loc_5);
                this._aMaterials.push(_loc_6);
                _loc_6.doubleSided = false;
                _loc_6.smooth = true;
                _loc_6.interactive = true;
                _loc_7 = new Plane(_loc_6, _loc_5.width, _loc_5.height, this.numPlaneSegments, this.numPlaneSegments);
                this._contentPlanes.addChild(_loc_7);
                this._viewport.getChildLayer(_loc_7);
                this._dPlanes[_loc_5] = _loc_7;
                this._dMaterialIndexes[_loc_5] = _loc_4;
                if (_loc_4 > 0)
                {
                    if (_loc_5.width != this._aMaterialMovies[_loc_4].width)
                    {
                        _loc_3 = _loc_3 + (_loc_5.width - this._aMaterialMovies[_loc_4].width) / 2;
                    }
                }
                _loc_7.x = _loc_3;
                _loc_3 = _loc_3 + (_loc_5.width + 15);
                if (this._aButtonData[_loc_4].title && _loc_5.title_txt)
                {
                    _loc_5.title_txt.text = this._aButtonData[_loc_4].title.toUpperCase();
                }
                _loc_4++;
            }
            this.setButtonListeners();
            this._headerPlane.x = this._headerPlane.x + (this._header_mc.width - this._aMaterialMovies[0].width) / 2;
            this._headerPlane.y = this._aMaterialMovies[0].height / 2 + this._header_mc.height / 2 + 12;
            if (this.enableBackButton)
            {
                this._backButton_mc = new BackButton();
                this._backButton_mc.bg_mc.width = this._aMaterialMovies[0].width;
                this._backButton_mc.content_mc.x = this._aMaterialMovies[0].width / 2 - this._backButton_mc.content_mc.width / 2;
                _loc_8 = new MovieMaterial(this._backButton_mc, true, true, true);
                _loc_8.doubleSided = true;
                _loc_8.smooth = true;
                _loc_8.interactive = true;
                _loc_8.animated = true;
                this._backButton = new Plane(_loc_8, this._backButton_mc.width, this._backButton_mc.height, 4, 4);
                addChild(this._backButton);
                this._viewport.getChildLayer(this._backButton);
                this._backButton.z = 50;
            }
            if (this._initialIndex >= 0)
            {
                this.selectPlane(this._initialIndex);
            }
            return;
        }// end function

        public function next(param1:Boolean = false)
        {
            if (this._selectedIndex < this._aMaterialMovies.length)
            {
                this.selectPlane(this._selectedIndex + 1);
            }
            return;
        }// end function

        public function get length()
        {
            return this._aMaterialMovies.length;
        }// end function

        public function optimizePlanes(event:Event = null, param2:Boolean = true):void
        {
            this.setMaterialAnimations(!param2);
            return;
        }// end function

        public function get selectedIndex()
        {
            return this._selectedIndex;
        }// end function

        public function setMaterialInteractions(param1:Boolean = true)
        {
            this._materialInteractions = param1;
            var _loc_2:int = 0;
            while (_loc_2 < this._aMaterialMovies.length)
            {
                
                _loc_2++;
            }
            return;
        }// end function

        public function previous(param1:Boolean = false)
        {
            if (this._selectedIndex > 0)
            {
                this.selectPlane(this._selectedIndex-1);
            }
            return;
        }// end function

        public function setMaterialAnimations(param1:Boolean = true)
        {
            this._materialAnimations = param1;
            var _loc_2:int = 0;
            while (_loc_2 < this._aMaterialMovies.length)
            {
                
                this._aMaterials[this._dMaterialIndexes[this._aMaterialMovies[_loc_2]]].animated = this._materialAnimations;
                _loc_2++;
            }
            return;
        }// end function

    }
}
